/*
 * Copyright 2013 Fabian Gilson. This file is part of Online Library academic project and it is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License version 3 as published by the Free Software Foundation. It is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details. see <http://www.gnu.org/licenses/>.
 */

package unamur.iodass.catalog;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;


import org.apache.log4j.Logger;

import unamur.iodass.communication.messages.Book;



/**
 * This class parses a CSV file containing books and fill in Book structure classes
 * 
 * @see org.onlinelibrary.parser.Book
 * @author Fabian Gilson
 */
public class CSVBookParser {

	private static final Logger logger = Logger.getLogger(CSVBookParser.class);



	/**
	 * Open the CSV file
	 * 
	 * @param file
	 *           the path (full or relative) to the CSV file
	 * @param charset
	 *           the character set of the CSV file
	 * @return the reader opened at the beginning of the file, null if the file does not exist
	 */
	public BufferedReader open(String file, Charset charset) {
		try {
			BufferedReader buffer = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
			return buffer;
		} catch (FileNotFoundException e) {
			logger.error(e.getClass().getSimpleName() + " " + e.getMessage());
			return null;
		}
	}

	/**
	 * Parse the whole CSV file from the given reader and returns an array of books
	 * 
	 * @param reader
	 *           reader opened on a CSV file
	 * @return an array of books
	 * @see org.onlinelibrary.parser.Book
	 */
	public ArrayList<Book> parse(BufferedReader reader) {
		ArrayList<Book> books = new ArrayList<Book>();
		try {
			while (reader.ready()) {
				books.add(parseLine(reader));
			}
		} catch (IOException e) {
			// do nothing, just return what we read until now (can be empty) 
		}
		return books;
	}

	/**
	 * Parse one line from CSV file from given reader
	 * 
	 * @param reader
	 *           reader opened on a CSV file
	 * @return the read book or null if a field of the book is invalid
	 * @see org.onlinelibrary.parser.Book
	 * @throws IOException
	 *            if an error occurred when reading the file
	 */
	public Book parseLine(BufferedReader reader) throws IOException {
		String read;
		String[] values;
		Book book = null;
		try {
			if ((read = reader.readLine()) != null) {
				values = read.split(";");
				book = new Book(values[0], values[1], values[2], Long.parseLong(values[3]));
			}
		} catch (ArrayIndexOutOfBoundsException e ) {
			logger.error(e.getClass().getSimpleName() + " " + e.getMessage());

		} catch (NumberFormatException e) {
			logger.error(e.getClass().getSimpleName() + " " + e.getMessage());
		}
		return book;
	}
}
